<template>
  <div class="login">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="管理员密码" prop="pw">
        <el-input type="password" v-model="ruleForm.pw" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class='button' type="primary" @click.stop="submitForm('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'login',
  data () {
    let validatePW = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('密码不能为空'))
      } else if (value !== 'admin') {
        return callback(new Error('密码错误'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pw: ''
      },
      rules: {
        pw: [{ validator: validatePW, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm (formName) {
      const _this = this
      this.$refs[formName].validate(valid => {
        if (valid) {
          // console.log(_this.ruleForm, '校验通过')
          window.sessionStorage.setItem('username', 'admin')
          _this.$router.push({ path: 'add' })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.login {
  width: 600px;
  padding-top: 100px;
  margin: auto;
  .button {
    width: 100%;
  }
}
</style>
